package com.yiwei.zhh.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.zhh.R;

import com.yiwei.zhh.adapter.MyFundDetailAdapter;
import com.yiwei.zhh.api.callback.MyFundContract;
import com.yiwei.zhh.api.requestInfo.MyFundRequest;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.model.MoneyLogModel;
import com.yiwei.zhh.util.OnLoadeMoreListener;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.view.CircularProgress;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/5/27.
 */

public class MyFundDetailActivity extends BaseActivity implements MyFundContract.View, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.list)
    RecyclerView list;
    MyFundDetailAdapter adapter;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.nothing)
    TextView nothing;
//    @BindView(R.id.progressBar)
//    CircularProgress loading;
    private MyFundRequest api;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            api.getMyFund(page, 10);
        }
    };
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_point_log_detail);
        unbinder = ButterKnife.bind(this);
        baseInitView(this, "PointLogDetailActivity", Const.ViewType.ACTIVITY);
        initView();
    }

    private void initView() {
        setBackIntent();
        setTitleText("交易明细");
        refresh.setOnRefreshListener(this);
        api = MyFundRequest.create();
        api.attachView(this);
        adapter = new MyFundDetailAdapter(this);
        list.setAdapter(adapter);
        list.addOnScrollListener(onLoadeMoreListener);
        api.getMyFund(1, 10);
//        adapter = new DealDetaiAdapterNew(MyFundDetailActivity.this, model.getData(), 1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        if (refresh != null && refresh.isRefreshing()) {
            refresh.setRefreshing(false);
        }
    }

    @Override
    public void onRequestStart() {
        if (refresh != null) {
            refresh.setRefreshing(true);
        }
    }

    @Override
    public void getMyFundSuccess(int pageIndex, MoneyLogModel model) {
        if (pageIndex <= model.pager.getAllPages()) {
            adapter.add(model.data, pageIndex == 1);
        } else {
            ToastUtil.makeText(getApplicationContext(), getResources().getString(R.string.no_more_date_text), Toast.LENGTH_SHORT).show();
        }
        if (adapter.getmSource().size() == 0 && model.data.size() <= 0) {
            nothing.setVisibility(View.VISIBLE);
        } else {
            nothing.setVisibility(View.GONE);
        }
    }

    @Override
    public void onRefresh() {
        onLoadeMoreListener.setPage(1, 0, false);
        api.getMyFund(1, 10);
    }

    /**
     * 是否显示加载框
     *
     * @param b
     */
//    private void showLoading(boolean b) {
//        if (b) {
//            if (loading != null) loading.setVisibility(View.VISIBLE);
//        } else {
//            if (loading != null) loading.setVisibility(View.GONE);
//        }
//    }
}
